﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ADO;

using System.Data.OleDb;

using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Web.UI.MobileControls;  


namespace Service.Email
{
    /// <summary>
    ///Cmassagesend 的摘要说明
    /// </summary>
    public class Cmassagesend:Cmassageload
    {
        Cmassageload boot = new Cmassageload();
        public Cmassagesend()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public void Sendmassage(string email)
        {

            DataTable tb;
            bool ex;//判断数据库操作是否成功
            string sql;//数据库操作语句
            string str;//数据库连接参数
            str = "Server=YourSQLServer;user id=sa;password=;initial catalog=Northwind;Connect Timeout=30";
            DataBaseAccess st = new DataBaseAccess(str);
            sql = "select * from 视图 where sort=0";
            tb = st.GetDataTable(sql);
            string from = tb.Rows[0][0].ToString();
            string password = tb.Rows[1][0].ToString();

           
            MailAddress MessageFrom = new MailAddress(from);
            string MessageTo = email;
            string MessageSubject = boot.m_massagetitle;
            string MessageBody = boot.m_massage;
            if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody,password))
            {
                //MessageBox.Show("OK");
            }
            else
            {
                //MessageBox.Show("NO");
            } 




            //根据获得邮箱将信息按照固定格式发送
        }
        public static bool Send(MailAddress Messagefrom, string MessageTo, string MessageSubject, string MessageBody,string Password)
        {
            MailMessage message = new MailMessage();
            message.From = Messagefrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;  //发送邮件的优先等级
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.qq.com";              //指定发送邮件的服务器地址或IP
            sc.Port = 25;                          //指定发送邮件端口
            sc.UseDefaultCredentials = true;
            sc.EnableSsl = true;
            sc.Credentials = new System.Net.NetworkCredential(message.From.User, Password); //指定登录服务器的用户名和密码
            try
            {
                sc.Send(message);      //发送邮件
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.Message);
                return false;
            }
            return true;
        }

        public void Getsolution(int sort,string massage,string massagetitle)
        {
            bool exs;
            Cmassageload boot = new Cmassageload(sort, massage, massagetitle);
            exs=boot.Savemassage(sort,massage,massagetitle);
            if (exs)
            {
                //MessageBox.Show("SAVEOK");
            }
            else
            {
                //MessageBox.Show("SAVENO");
            }
            Cmassagesend sd = new Cmassagesend();
            DataTable tb;
            bool ex;//判断数据库操作是否成功
            string sql;//数据库操作语句
            string str;//数据库连接参数
            str = "Server=YourSQLServer;user id=sa;password=;initial catalog=Northwind;Connect Timeout=30";
            DataBaseAccess st = new DataBaseAccess(str);
            sql = "select * from 视图 where sort=boot.sort";
            tb = st.GetDataTable(sql);
            for (int i = 0; i <= tb.Rows.Count; i++)
            {
               // email = tb.Row[0][i];
               // sd.Sendmassage(email);
            }
            
        }
       
    }
}
